package com.hzw.saas.web.admin.job.task;

import java.util.concurrent.CountDownLatch;

import cn.hutool.core.date.SystemClock;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2021/5/7 13:49
 */
@Slf4j
public abstract class AbstractTaskService implements Runnable {
    private CountDownLatch countDownLatch;

    public AbstractTaskService(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            long startTime = SystemClock.now();
            execute();
            long times = SystemClock.now() - startTime;
            log.info("并发任务{}执行完毕,耗时: {} 毫秒", getTaskName(), times);
        } finally {
            countDownLatch.countDown();
        }
    }

    protected abstract void execute();

    protected String getTaskName() {
        return Thread.currentThread().getName();
    }
}
